<?php
namespace jy_baojdai;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class task extends \plugin{
	//加权代理
	public function up_redfee($setting=array()){
		if($setting){
			$this->setting=$setting;
		}
        $daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
        $lastdaytime = $daytime-24*3600;
		$isfeelog=pdo_get('jy_baojdai_feelog',array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime));
        if($isfeelog['id']){
            return false;
        }
		if($this->setting['fee5_all']>0){
			$orderprice=$this->setting['fee5_all'];
		}else{
			$orderprice=pdo_getsum('jy_weishop_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'createtime between'=>array($lastdaytime,$daytime)),'price');
        }
		if($orderprice<=0){
            return false;
        }
        $levels=$this->getall('level',array('uniacid'=>$_SESSION['uniacid'],'fee_rate5 <>'=>''),array('id','title','level','fee_rate5'),'id');
        $default_level=$this->plugin_setting('default_level');
        $levels['0']=array('id'=>0,'title'=>'默认等级','fee_rate5'=>$default_level['fee_rate5']);
        $log=array(
            'orderprice'=>$orderprice,
        );
		foreach($levels as $lv){
			$members2=array();
			$is_all5ids=$this->getall('level',array('level >'=>$lv['level'],'uniacid'=>$_SESSION['uniacid'],'is_all5'=>1),array('id'),'id');
			$is_all5ids=$is_all5ids?array_keys($is_all5ids):0;
            $log['level_id']=$lv['id'];
            if (substr($lv['fee_rate5'], -1) == '%') {
                $jifee = ($orderprice * substr($lv['fee_rate5'], 0, -1)) / 100;
            } else {
                $jifee = $lv['fee_rate5'];
            }
            if(!$jifee){
              continue;
            }
            $members=$this->getall('member',array('level_id'=>$lv['id'],'uniacid'=>$_SESSION['uniacid']),array('redfee','mid'));
			$log['num']=count($members);
			if($is_all5ids && $lv['id'] && !in_array($lv['id'],$is_all5ids)){
				$members2=$this->getall('member',array('level_id'=>$is_all5ids,'uniacid'=>$_SESSION['uniacid']),array('redfee','mid'));
				$log['num'] +=count($members2);
			}
            if(!$log['num']){
                continue;
            }
            $log['jifee']=$jifee;
            $log['onefee']=$jifee/$log['num'];
            if(!$log['onefee']){
                continue;
            }
            foreach($members as $mv){
				$fee=$log['onefee'];
				if($fee>0){
					$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$mv['mid'],
							'ordersn'=>'管理奖',
							'money'=>$fee,
							'createtime'=>TIMESTAMP,
							'day'=>date('Y-m-d',TIMESTAMP),
							'year'=>date('Y',TIMESTAMP),
							'status'=>1,
							'type'=>5,
					);
					$this->insert('commission',$new_commission);
				}
            }
			if($members2){
				foreach($members2 as $mv){
					$fee=$log['onefee'];
					if($fee>0){
						$new_commission=array(
								'uniacid'=>$_SESSION['uniacid'],
								'mid'=>$mv['mid'],
								'ordersn'=>'管理奖(多拿)',
								'money'=>$fee,
								'createtime'=>TIMESTAMP,
								'day'=>date('Y-m-d',TIMESTAMP),
								'year'=>date('Y',TIMESTAMP),
								'status'=>1,
								'type'=>5,
						);
						$this->insert('commission',$new_commission);
					}
				}
			}
			$this->add_feelog($log,1);
        }
		return true;
	}
	public function add_feelog($log=array(),$type){
		$feelog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'level_id'=>$log['level_id'],
			'jifee'=>$log['jifee'],
            'onefee'=>$log['onefee'],
			'num'=>$log['num'],
			'orderprice'=>$log['orderprice'],
			'type'=>$type,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('feelog',$feelog);
		return true;
	}
	public function up_daystime(){
		$commissions=$this->getall('commission',array('day'=>'','uniacid'=>$_SESSION['uniacid']),array('id','mid','createtime'));
		foreach($commissions as $mv){
			$update=array(
				'day'=>date('Y-m-d',$mv['createtime']),
				'year'=>date('Y',$mv['createtime']),
			);
			$this->update('commission',$update,array('id'=>$mv['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
}